package cn.pzhu.pserson.util;

import com.alibaba.fastjson.JSONObject;

import java.security.MessageDigest;
import java.util.*;

public class jssdk {

    static final String appid = "wxaa444b67dbe61dc1";
    static final String appsecret = "5f569dd55939277674b5e41919ec6d61";
//    static final String appid = "wx95c553407c03a5e2";
//    static final String appsecret = "1b3850b3a0873b983e59b25a5a90591b";
    static String access_token = "";
    static String jsapi_ticket = "";
    static Date time = null;
    public static void getToken_getTicket() throws Exception {

        if(time != null){
            Date now = new Date();
            long house = (now.getTime() - time.getTime()) / 1000 / 60 / 60;
            if(house < 1.5){
                return;
            }
        }

        Map<String, String> params = new HashMap<String, String>();
        //获取token执行体
        params.put("grant_type", "client_credential");
        params.put("appid", appid);
        params.put("secret", appsecret);
        String jstoken = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token", params);
        jssdk.access_token = JSONObject.parseObject(jstoken).getString("access_token");
//                  String access_token = JSONObject.fromObject(jstoken).getString(
//                                  "access_token"); // 获取到token并赋值保存

//                  GlobalConstants.interfaceUrlProperties.put("access_token", access_token);
        if(jssdk.access_token != null){
            time = new Date();
        } else {
            return;
        }

        //获取jsticket的执行体
        params.clear();
        params.put("access_token", access_token);
        params.put("type", "jsapi");
        String jsticket = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket", params);
        jssdk.jsapi_ticket= JSONObject.parseObject(jsticket).getString("ticket");


        System.out.println("jsapi_ticket================================================" + jsapi_ticket);
//      System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"token为=============================="+access_token);
    }


    public static String getOpenid (String code) {
        Map<String, String> result = new HashMap<String, String>();
        Map<String, String> params = new HashMap<String, String>();
        params.put("appid", appid);
        params.put("secret", appsecret);
        params.put("code", code);
        params.put("grant_type", "authorization_code");

        String response = HttpUtils.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token",params);

        return JSONObject.parseObject(response).getString("openid");
    }


    public static String CreatenTimestamp()
    {
        long timestamp = new Date().getTime()/1000;

        return String.valueOf(timestamp);
    }

    /**
     17      * @Description: 前端jssdk页面配置需要用到的配置参数
     18      * @param @return hashmap {appid,timestamp,nonceStr,signature}
     19      * @param @throws Exception
     20      * @author gede
     21      */
     public static HashMap<String, String> jsSDK_Sign(String url) throws Exception {
                 jssdk.getToken_getTicket();
                 String nonce_str = create_nonce_str();
                 String timestamp= jssdk.CreatenTimestamp();
                 String jsapi_ticket= jssdk.jsapi_ticket;
                 // 注意这里参数名必须全部小写，且必须有序
                 String  string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str
                         + "&timestamp=" + timestamp  + "&url=" + url;
                 MessageDigest crypt = MessageDigest.getInstance("SHA-1");
                 crypt.reset();
                 crypt.update(string1.getBytes("UTF-8"));
                 String signature = byteToHex(crypt.digest());
                 HashMap<String, String> jssdk=new HashMap<String, String>();
                 jssdk.put("appId", appid);
                 jssdk.put("timestamp", timestamp);
                 jssdk.put("nonceStr", nonce_str);
                 jssdk.put("signature", signature);
//                 jssdk.put("access_token", access_token);
                 return jssdk;

             }

            private static String byteToHex(final byte[] hash) {
                 Formatter formatter = new Formatter();
                for (byte b : hash) {
                        formatter.format("%02x", b);
                     }
                 String result = formatter.toString();
                 formatter.close();
                 return result;
             }

             private static String create_nonce_str() {
                 return UUID.randomUUID().toString();
             }
}
